package cn.yikaojiuguo.student.support.utils;

import android.app.Activity;

import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.support.simple.UrlConstants;
import cn.yikaojiuguo.student.support.volley.Response;

/**
 * Created by cfan_yang on 2016/1/8.
 */
public class QiniuUploadUtils {

    private static UploadManager mUploadManager = new UploadManager();

    public static void uploadSingleImg(final Activity activity, String getTokenUrl, final String
            imgPath, final UpCompletionHandler completionHandler, final UpProgressHandler
                                               progressHandler) {
        RequestUtil.requestGet(getTokenUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null && response.optBoolean("success")) {
                    String token = response.optString("message");
                    String key = Util.getUUID() + ".jpg";
                    YiKaoApplication.putUploadImgName(key);
                    Map<String, String> params = new HashMap<String, String>(1);
                    params.put("x:mid", YiKaoApplication.getUserId());
                    LogUtil.showDebugLog("TOKEN", token);
                    LogUtil.showInfoLog("NAME", key);
                    mUploadManager.put(imgPath, key, token, completionHandler, new UploadOptions
                            (params, null, false, progressHandler, null));
                }
            }
        }, null, activity.getClass().getSimpleName());
    }

    public static void uploadAskImgs(final Activity activity, final List<String>
            pics, final UpCompletionHandler completionHandler, final UpProgressHandler
                                             progressHandler) {
        RequestUtil.requestGet(UrlConstants.GET_ASK_TOKEN + "&mid=" + YiKaoApplication.getUserId
                (), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null && response.optBoolean("success")) {
                    String token = response.optString("message");
                    Map<String, String> params = new HashMap<String, String>(2);
                    params.put("x:mid", YiKaoApplication.getUserId());
                    params.put("x:etype", 1 + "");
                    LogUtil.showInfoLog("TOKEN", token);
                    //文件命名方式为 UUID +　顺序
                    int order = 0;
                    for (String imgPath : pics) {
                        String key = Util.getUUID() + "-" + order + ".jpg";
                        LogUtil.showInfoLog("key", key);
                        mUploadManager.put(imgPath, key, token, completionHandler, new UploadOptions
                                (params, null, false, progressHandler, null));
                        order++;
                    }
                }
            }
        }, null, activity.getClass().getSimpleName());
    }

    public static void uploadAskVoice(final Activity activity, final UpCompletionHandler
            completionHandler, final UpProgressHandler progressHandler, final String audiotime) {
        RequestUtil.requestGet(UrlConstants.GET_ASK_TOKEN + "&mid=" + YiKaoApplication.getUserId
                (), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if(response != null && response.optBoolean("success")) {
                    String token = response.optString("message");
                    Map<String, String> params = new HashMap<String, String>(2);
                    params.put("x:mid", YiKaoApplication.getUserId());
                    params.put("x:etype", 2 + "");
                    params.put("x:audiotime", audiotime);
                    String key = Util.getUUID() + ".amr";
                    mUploadManager.put(YiKaoApplication.getCacheVoicePath() + "/ask.amr", key, token, completionHandler, new UploadOptions
                            (params, null, false, progressHandler, null));
                }
            }
        }, null, activity.getClass().getSimpleName());
    }

}
